package LeetCode;

public class LC_005_LongestPalindromicSubstring_1 {
    public static void main(String[] args) {
        String s = "123321";
        System.out.println(longestPalindrome(s));
    }

    private static String longestPalindrome(String s) {
        if (s.isEmpty())
            return null;
        if (s.length() == 1)
            return s;
        String longest = s.substring(0, 1);
        for (int i = 0; i < s.length(); i++) {
            String tmp = helper(s, i, i);
            longest = tmp.length() > longest.length() ? tmp : longest;

            tmp = helper(s, i, i + 1);
            longest = tmp.length() > longest.length() ? tmp : longest;
        }
        return longest;
    }

    private static String helper(String s, int begin, int end) {
        while (begin >= 0 && end <= s.length() - 1 && s.charAt(begin) == s.charAt(end)) {
            begin--;
            end++;
        }
        return s.substring(begin + 1, end);
    }
}
